﻿using System;
using Skynet.CommonLibrary.ThreadRefreshTime.Interface;

namespace Skynet.CommonLibrary.ThreadRefreshTime.Extension
{
    /// <summary>
    /// 刷新元素扩展
    /// </summary>
    ///<remarks>
    /// 作者：         李志强
    /// 开发时间：  2011-03-28
    /// </remarks>
   public static  class RefreshTimerExtension
    {
        /// <summary>
        /// 刷新验证
        /// </summary>
        /// <param name="reftime"></param>
        /// <param name="failed"></param>
        /// <param name="failedAction"> </param>
        public static void ExecutiveRefresh (this IRefreshTimer reftime,Action<IRefreshTimer,Exception> failed,Action failedAction)
       {
           try
           {
               if (reftime is IRefreshTaskTimer)
               {
                   #region IRefreshTaskTimer
                   IRefreshTaskTimer rtasktime = reftime as IRefreshTaskTimer;

                   //1:判断结束时间
                   //如果结束时间已到 则抛出异常 已通知任务结束
                   if (rtasktime.TaskEndTime.ToString("yyyyMMdd") != DateTime.MinValue.ToString("yyyyMMdd"))
                   {
                       if (DateTime.Now >= rtasktime.TaskEndTime)
                       {
                           if (failedAction != null)
                           {
                               failedAction();
                           }
                           failed(rtasktime, new Exception("时间超出结束"));

                           return;
                       }
                   }

                   //2:判断执行条件
                   //条件以委托的形式放在外部  如果不符合 则不执行刷新操作
                   if (rtasktime.TaskConditions != null && !rtasktime.TaskConditions(DateTime.Now))
                   {
                       return;
                   }
                   //3:判断执行次数
                   //没执行一次进行累加
                   if (rtasktime.RefreshNumber != 0)
                   {
                       if (rtasktime.CurrentRefreshNumber != rtasktime.RefreshNumber)
                       {
                           rtasktime.CurrentRefreshNumber++;
                       }
                       else
                       {
                           if (failedAction != null)
                           {
                               failedAction();
                           }
                           failed(rtasktime, new Exception("执行完毕"));

                           return;
                       }
                   }
                   #endregion
               }
               //4:执行刷新操作
               reftime.Refresh(failed);
               reftime.LastRefreshTime = DateTime.Now;

           }
           catch (Exception ex)
           {
               //处理不必要的异常  并控制是继续执行还是立刻取消
               if (failed != null)
               {
                   failed(reftime, ex);
               }
           }
       }

       /// <summary>
       /// 写DEBUG日志
       /// </summary>
       /// <param name="reftime"></param>
       public static void WriteDebugLog(this IRefreshTimer reftime)
       {
           if (reftime is IRefreshTaskTimer)
           {
               IRefreshTaskTimer rtt = reftime as IRefreshTaskTimer;
               log4net.LogManager.GetLogger("RefreshTimerLog").Debug(
                   string.Format("[{0}]{1}->{2}:{3}/{4} <{5} {6} {7}>",
                   rtt.RefreshID,
                   rtt.TaskStartTime.ToString("yyyy-MM-dd HH:mm:ss"),
                   rtt.TaskEndTime.ToString("yyyy-MM-dd HH:mm:ss"),
                   rtt.CurrentRefreshNumber,
                   rtt.RefreshNumber,
                   rtt.ExecutionTime.ToString("yyyy-MM-dd HH:mm:ss"),
                   rtt.LastRefreshTime.ToString("yyyy-MM-dd HH:mm:ss"),
                   rtt.Description
                   ));
           }
           else
           {
               log4net.LogManager.GetLogger("RefreshTimerLog").Debug(
                 string.Format("[{0}] <{1} {2} {3}>",
                 reftime.RefreshID,
                 reftime.ExecutionTime.ToString("yyyy-MM-dd HH:mm:ss"),
                 reftime.LastRefreshTime.ToString("yyyy-MM-dd HH:mm:ss"),
                 reftime.Description
                 ));
           }
       }

       /// <summary>
       /// 写信息日志
       /// </summary>
       /// <param name="reftime"></param>
       /// <param name="msg"></param>
       public static void WriteInfoLog(this IRefreshTimer reftime,string msg)
       {
           log4net.LogManager.GetLogger("RefreshTimerLog").Debug(
                            string.Format("[{0}]->{1}",
                            reftime.RefreshID,
                            msg
                            ));
       }

       /// <summary>
       /// 写错误日志
       /// </summary>
       /// <param name="reftime"></param>
       /// <param name="ex"></param>
       public static void WriteErrorLog(this IRefreshTimer reftime, Exception ex)
       {
           log4net.LogManager.GetLogger("RefreshTimerLog").Error(reftime.RefreshID, ex);
       }
    }
}
